package model;

import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;

import action.OpenAction;
import controller.MainController;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import service.MouseEvenHandler;

public class FileNode extends Label {
	private MainController mainScene;
	private Fileos fileos;
	private Image image;
	private ImageView imageView;
	private Text pictureName;
	private FileNode fileNode = this;

	public BooleanProperty selected = new SimpleBooleanProperty();

	protected static ArrayList<File> selectedPictureFiles = new ArrayList<>();
	protected static ArrayList<FileNode> selectedPictures = new ArrayList<>();
	protected static ArrayList<FileNode> cutedPictures = new ArrayList<>();

	public FileNode(Fileos fileos, MainController mainUIController) throws MalformedURLException {
		this.fileos = fileos;
		this.mainScene = mainUIController;
		initData();
		addPictureNodeListener();
		new MyContextMenu(this, mainScene, true);
	}

	private void initData() throws MalformedURLException {

		this.setGraphicTextGap(10);
		this.setPadding(new Insets(1, 1, 1, 1));
		this.setContentDisplay(ContentDisplay.TOP);
		this.setPrefSize(110, 110);

		this.image = new Image(((File) fileos.getImageFile()).toURI().toURL().toString(), 100, 100, true, true);// 链接，长宽，保留原始长宽比
		this.imageView = new ImageView(image);
		this.pictureName = new Text(fileos.getName());
		this.setText(pictureName.getText());
		this.setGraphic(imageView);// 设置图片
		fileNode.setId("fileNode");
	}

	public Image getImage() {
		try {
			return image = new Image(((File) fileos.getImageFile()).toURI().toURL().toString());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return image;

	}

	public File getImageFile() {
		return (File) this.fileos.getImageFile();
	}

	public Fileos getPictureFile() {
		return fileos;
	}

	public String getURL() {
		try {
			return ((File) this.fileos.getImageFile()).toURI().toURL().toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public ImageView getImageView() {
		return this.imageView;
	}

	public static ArrayList<File> getSelectedPictureFiles() {
		return selectedPictureFiles;
	}

	public static ArrayList<FileNode> getSelectedPictures() {
		return selectedPictures;
	}

	public static void setCutedPictures(ArrayList<FileNode> cutedPictures) {
		FileNode.cutedPictures = cutedPictures;
	}

	public static void addCutedPictures(FileNode pNode) {
		FileNode.cutedPictures.add(pNode);
	}

	public static ArrayList<FileNode> getCutedPictures() {
		return cutedPictures;
	}

	public static void clearCutedPictures() {
		cutedPictures.removeAll(cutedPictures);
	}

	public void setSelected(boolean value) {
		boolean istrue = selected.get();
		selected.set(value);
		if (selected.get() && !istrue)
			selectedPictures.add(this);
		else if (istrue && !selected.get())
			selectedPictures.remove(this);
		// System.out.println(selectedPictures.size());
		for (FileNode pNode : selectedPictures) 
		{
		//	System.out.println("该图片的路径是:"+pNode.getURL());
			String ss = new String(pNode.getURL());
			
			String sss = ss.substring(6);
			System.out.println("该文件的路径是:  "+sss);
			
			File f = new File(sss);
			System.out.println("该文件的大小是："+f.length());
			//System.out.println(f.length()/1024);
		}	
		mainScene.getTextTwo().setText("已选中 " + selectedPictures.size() + " 张图片");
	}

	public static void clearSelected() {
		for (FileNode pNode : selectedPictures) {
			pNode.selected.set(false);
		}
		selectedPictures.removeAll(selectedPictures);
	}

	private void addPictureNodeListener() {
		selected.addListener(new InvalidationListener() {
			@Override
			public void invalidated(Observable observable) {
				if (selected.get()) {
					fileNode.setStyle("-fx-background-color:#a7a7a7;");
//					mainScene.getText().setText("");
					mainScene.getTextTwo().setText("已选中 0  个文件");
				} else {
					fileNode.setStyle("-fx-background-color:transparent;");
//					System.out.println(selectedPictures.size()+"--");
					mainScene.getTextTwo().setText("已选中 0  个文件");
				}
			}
		});
		this.setOnMouseEntered((MouseEvent e) -> {
			if (!selected.get())
				this.setStyle("-fx-background-color:linear-gradient(to bottom,#3e4147 1%,  #a7a7a7 98%);");
//			    mainScene.getText().setText("");

		});
		this.setOnMouseExited((MouseEvent e) -> {
			if (!selected.get())
				this.setStyle("-fx-background-color:transparent;");

		});

		this.addEventHandler(MouseEvent.MOUSE_CLICKED, new MouseEvenHandler(this, pictureFile));
	}

	public void openAction() {
//		mainScene.setList();
		new OpenAction();
	}
}